package com.lkd.handler;

import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Objects;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-07-19 14:24
 */
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class RepairCompleteHandler implements com.lkd.business.MsgHandler {

    @Autowired
    private VendingMachineService vmService;

    @Override
    @Transactional
    public void process(String jsonMsg) throws IOException {
        TaskCompleteContract contract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);

        //判断是投放工单
        if(Objects.equals(contract.getTaskType(), VMSystem.TASK_TYPE_DEPLOY)){
             //更改售货机状态为运营
//            vmService.update(Wrappers.<VendingMachineEntity>lambdaUpdate()
//                    .set(VendingMachineEntity::getVmStatus, VMSystem.VM_STATUS_RUNNING)
//                    .eq(VendingMachineEntity::getInnerCode, contract.getInnerCode()));
//
            vmService.updateStatus(contract.getInnerCode(),VMSystem.VM_STATUS_RUNNING);
        }else if(Objects.equals(contract.getTaskType(), VMSystem.TASK_TYPE_REVOKE)){
            //更改售货机状态为撤机
//            vmService.update(Wrappers.<VendingMachineEntity>lambdaUpdate()
//                    .set(VendingMachineEntity::getVmStatus, VMSystem.VM_STATUS_REVOKE)
//                    .eq(VendingMachineEntity::getInnerCode, contract.getInnerCode()));
            vmService.updateStatus(contract.getInnerCode(),VMSystem.VM_STATUS_REVOKE);
        }
    }
}
